import  matplotlib.pyplot as plt
import pandas as pd

# x = [1,2,3,5,7,9]
# plt.boxplot(x)

# x1 = [1,2,3,5,7,9]
# x2 = [10,22,13,15,8,19]
# x3 = [18,31,18,19,14,29]
# plt.boxplot([x1,x2,x3])

df = pd.read_excel(r'./data/tips.xlsx')
plt.boxplot(
    x = df['总消费'], # 指定绘制箱线图的数据
    whis=1.5 , # 指定1.5倍的四分差位
    patch_artist= True , # 填充颜色
    showmeans=True , # 显示均值
    boxprops= {'facecolor':'RoyalBlue'},
    flierprops={'markerfacecolor':'red','markeredgecolor':'red','markersize':3},
    medianprops={'linestyle':'--','color':'orange'},
    meanprops={'marker':'h','markerfacecolor':'black','markersize':8},
    labels=['']
)
plt.show()

# 计算四分位数和上四分位
Q1 = df['总消费'].quantile(q=0.25)
Q3 = df['总消费'].quantile(q=0.75)
# 基于1.5倍的四分位差计算上下限对应值
low_limit = Q1 - 1.5*(Q3-Q1)
up_limit = Q3 - 1.5*(Q3-Q1)

# 查找异常值
val = df['总消费'][(df['总消费'] > up_limit) | (df['总消费'] < low_limit)]
print('异常值如下：')
print(val)
